﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RhombusWebMVC.Models;

namespace RhombusWebMVC.Controllers
{
	public class WorkItemController : Controller
	{
		private IWorkItemRepository<Guid, WorkItemViewModel> WorkItemRepository;

		protected override void Initialize(System.Web.Routing.RequestContext requestContext)
		{
			base.Initialize(requestContext);

			if (WorkItemRepository == null)
			{
				WorkItemRepository = new WorkItemRepository();
			}
		}

		public ActionResult Index()
		{
			return View();
		}

		public ActionResult Create()
		{
			return View();
		}

		[HttpPost]
		public ActionResult Create(WorkItemViewModel model)
		{
			if (!ModelState.IsValid)
			{
				return View(model);
			}

			Guid id = WorkItemRepository.CreateWorkItem(model, User.Identity.Name);

			return RedirectToAction("Edit", new { id = id });
		}

		public ActionResult Edit(Guid id)
		{
			var model = WorkItemRepository.GetWorkItem(id);

			return View(model);
		}

		[HttpPost]
		public ActionResult Edit(Guid id, WorkItemViewModel model)
		{
			if (!ModelState.IsValid)
			{
				return View(model);
			}

			WorkItemRepository.EditWorkItem(id, model, User.Identity.Name);

			return Edit(id);
		}

		public ActionResult WorkItemList()
		{
			var model = WorkItemRepository.GetWorkItems();

			return PartialView(model);
		}
	}
}
